home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 051-075 / 051 / sq.usq / tr1.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  1KB  |  70 lines

  1. #include <stdio.h>
  2. #include "sqcom.h"
  3. #include "sq.h"
  4. #define ERROR -1
  5. #define TRUE 1
  6. #define FALSE 0
  7.  
  8. /* First translation - encoding of repeated characters
  9.  * The code is byte for byte pass through except that
  10.  * DLE is encoded as DLE, zero and repeated byte values
  11.  * are encoded as value, DLE, count for count >= 3.
  12.  */
  13.  
  14. /* ver3.3 modified to eliminate all assignments within return(..). */
  15.  
  16. init_ncr()    /*initialize getcnr() */
  17. {
  18.     state = NOHIST;
  19. }
  20.  
  21. int
  22. getcnr(iob)
  23. FILE *iob;
  24. {
  25.     switch(state) {
  26.     case NOHIST:
  27.         /* No relevant history */
  28.         state = SENTCHAR;
  29.         lastchar = getc_crc(iob);
  30.         return (lastchar);   
  31.     case SENTCHAR:
  32.         /* Lastchar is set, need lookahead */
  33.         switch(lastchar) {
  34.         case DLE:
  35.             state = NOHIST;
  36.             return (0);    /* indicates DLE was the data */
  37.         case EOF:
  38.             return (EOF);
  39.         default:
  40.             for(likect = 1; (newchar = getc_crc(iob)) == lastchar && likect < 255; ++likect)
  41.                 ;
  42.             switch(likect) {
  43.             case 1:
  44.                 lastchar = newchar;
  45.                 return (lastchar);
  46.             case 2:
  47.                 /* just pass through */
  48.                 state = SENDNEWC;
  49.                 return (lastchar);
  50.             default:
  51.                 state = SENDCNT;
  52.                 return (DLE);
  53.             }
  54.         }
  55.     case SENDNEWC:
  56.         /* Previous sequence complete, newchar set */
  57.         state = SENTCHAR;
  58.         lastchar = newchar;
  59.         return (lastchar);
  60.     case SENDCNT:
  61.         /* Sent DLE for repeat sequence, send count */
  62.         state = SENDNEWC;
  63.         return (likect);
  64.     default:
  65.         puts("Bug - bad state\n");
  66.         exit(1);
  67.     }
  68.     return (0);    /*Won't get here but this'll make some compiler happy*/
  69. }
  70.